package cn.innosmart.aq.manager;

import com.tutk.p2p.ConnectionEntity;
import com.tutk.p2p.ResponseHandler;

import org.json.JSONObject;

import java.util.HashMap;

/**
 * Created by David on 2015/7/1.
 */
public class ClientManager extends BaseManager {
    private String Tag = "ClientManager";


    private static class ClientManagerInstance {
        private static final ClientManager instance = new ClientManager();
    }

    public static ClientManager getInstance() {
        return ClientManagerInstance.instance;
    }

    private ClientManager() {
    }

    // ClientManager.Register
    public boolean Register(ConnectionEntity connectionEntity, HashMap<Long, ResponseHandler> mReqRspHashMap, String UID, String params, long callid) {
        log(Tag, "Register. UID = " + UID + ", params = " + params);
        ResponseHandler handler = new ResponseHandler();
        handler.setCallback(mClientManagerRegisterCallBack);
        mReqRspHashMap.put(callid, handler);
        if (!SendCommand(connectionEntity, UID, "ClientManager.Register", params, callid)) {
            mReqRspHashMap.remove(callid);
            return false;
        } else {
            return true;
        }
    }


    private ResponseHandler.ResponseCallback mClientManagerRegisterCallBack = new ResponseHandler.ResponseCallback() {
        @Override
        public void onResponseCallback(ConnectionEntity connectionEntity, String message) {
            log(Tag, "ClientManager.Register get response=" + message);

        }
    };

    // ClientManager.Unregister
    public boolean Unregister(ConnectionEntity connectionEntity, HashMap<Long, ResponseHandler> mReqRspHashMap, String UID, String params, long callid) {
        log(Tag, "Unregister. UID = " + UID + ", params = " + params);
        ResponseHandler handler = new ResponseHandler();
        handler.setCallback(mClientManagerUnregisterCallBack);
        mReqRspHashMap.put(callid, handler);
        if (!SendCommand(connectionEntity, UID, "ClientManager.Unregister", params, callid)) {
            mReqRspHashMap.remove(callid);
            return false;
        } else {
            return true;
        }
    }

    private ResponseHandler.ResponseCallback mClientManagerUnregisterCallBack = new ResponseHandler.ResponseCallback() {
        @Override
        public void onResponseCallback(ConnectionEntity connectionEntity, String message) {
            log(Tag, "ClientManager.Unregister get response=" + message);

        }
    };

    // ClientManager.RefreshTimestamp
    public boolean RefreshTimestamp(ConnectionEntity connectionEntity, String params, long callid) {
        log(Tag, "RefreshTimestamp. UID = " + connectionEntity.getUid() + ", params = " + params);
        ResponseHandler handler = new ResponseHandler();
        handler.setCallback(mClientManagerUnregisterCallBack);
        connectionEntity.mReqRspHashMap.put(callid, handler);
        if (!SendCommand(connectionEntity, connectionEntity.getUid(), "ClientManager.RefreshTimestamp", params, callid)) {
            connectionEntity.mReqRspHashMap.remove(callid);
            return false;
        } else {
            return true;
        }
    }

    private ResponseHandler.ResponseCallback mClientManagerRefreshTimestampCallBack = new ResponseHandler.ResponseCallback() {
        @Override
        public void onResponseCallback(ConnectionEntity connectionEntity, String message) {
            log(Tag, "ClientManager.RefreshTimestamp get response=" + message);
        }
    };

    //    //{"type":"req","method":"ZWaveController.Reset","callid":1333446400,"params":{},"to":"device_core","source":"2f68f7370a66fb78"}
    public boolean Reset(ConnectionEntity connectionEntity, String params) {
        log(Tag, "Connect. UID = " + connectionEntity.getUid() + ", params = " + params);
        long callid = System.currentTimeMillis();
        if (!SendCommand(connectionEntity, connectionEntity.getUid(), "ZWaveController.Reset", params, callid)) {
            return false;
        } else {
            return true;
        }
    }

}
